package Action.user;

import Action.Sys.Setting;
import Bean.BookInfoEntity;
import Bean.LoanBookLogRecord;
import Bean.Log.OrderLog;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import org.hibernate.Session;
import tools.HibernateTools;

import java.text.ParseException;
import java.util.LinkedList;
import java.util.List;

import static tools.TimeTools.Remainder_days;
import static tools.TimeTools.timeStamp2Date;

// 暂停使用
public class UserOder extends ActionSupport {
    List<LoanBookLogRecord> records;
    LinkedList<Object[]> user_records=new LinkedList<>();
    int user_key;
    int order_key=-1;
    Session session;
    public String list_Oder() throws Exception{
        try {
            user_key = (int) ActionContext.getContext().getSession().get("userid");
        } catch (Exception e) {
            return LOGIN;
        }
        if (order_key==-1){
            return LOGIN;
        }
         session= HibernateTools.getSession();
        records=session.createQuery("from LoanBookLogRecord where userId=? order by id desc ").setParameter(0,user_key).getResultList();

        for (LoanBookLogRecord record : records) {
            user_records.addLast(new Object[]{record.getId(),bulid_order(record),record.getState()});
        }
        HibernateTools.commit(session);
        return SUCCESS;
    }
    //取消订单
    public String cancel_order () throws Exception {
        if (order_key==-1){
            list_Oder();
            return ERROR;
        }

        Session session=HibernateTools.getSession();
        LoanBookLogRecord loanBookLogRecord=session.get(LoanBookLogRecord.class, order_key);
        if (loanBookLogRecord==null)
        {
            list_Oder();
            return ERROR;
        }
        if (!loanBookLogRecord.getState().equals(Setting.LoanBook_state_reservation))
        {
            list_Oder();
            addActionError("订单已经生效无法取消");
            return ERROR;
        }
        BookInfoEntity bookInfoEntity=session.get(BookInfoEntity.class, loanBookLogRecord.getBookId());
        if (bookInfoEntity!=null){
            bookInfoEntity.setUsed(bookInfoEntity.getUsed()-1);
            session.update(bookInfoEntity);
        }
        loanBookLogRecord.setState(Setting.LoanBook_state_closed);
        session.update(loanBookLogRecord);


        OrderLog.addLog(loanBookLogRecord.getId(), "用户取消订单");
        HibernateTools.commit(session);
        list_Oder();
        return SUCCESS;
    }

    private String bulid_order(LoanBookLogRecord record) throws Exception{
        try {
            return "订单号:"+record.getId()+"   "+timeStamp2Date(record.getLoanTime(), "")+
                    "     "+session.get(BookInfoEntity.class, record.getBookId()).getBook_name()+"归还剩余天数"
           +(record.getDay()-Remainder_days(record.getLoanTime())) +"状态:";
        } catch (ParseException e) {
            return "订单记录生成失败,数据不合法";
        }
    }



    public LinkedList<Object[]> getUser_records() {
        return user_records;
    }

    public void setUser_records(LinkedList<Object[]> user_records) {
        this.user_records = user_records;
    }

    public int getUser_key() {
        return user_key;
    }

    public void setUser_key(int user_key) {
        this.user_key = user_key;
    }

    public int getOrder_key() {
        return order_key;
    }

    public void setOrder_key(int order_key) {
        this.order_key = order_key;
    }
}
